package com.volleyloadimage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private String strUrl = "https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=fc1aa3f7bc3eb13544c7b0bd9e25cfee/58ee3d6d55fbb2fb3f06d70d4f4a20a44623dc84.jpg";
    private Button btnHttpConnection;
    private Button btnImgRequest;
    private Button btnImgLoad;
    private Button btnImgNetwork;
    private ImageView ivImg;
    private NetworkImageView ivNetwork;
    private Handler handler;
    private ImgLoadCache imgLoadCache;
    private RequestQueue requestQueue;
    private static final int GET_IMG = 1001;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindView();
        requestQueue = Volley.newRequestQueue(MainActivity.this);
        imgLoadCache = new ImgLoadCache();

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == GET_IMG) {
                    Bitmap bitmap = (Bitmap) msg.obj;
                    ivImg.setImageBitmap(bitmap);
                    return true;
                }
                return false;
            }
        });

        btnHttpConnection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url = new URL(strUrl);
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            InputStream inputStream = httpURLConnection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                            Message message = Message.obtain();
                            message.what = GET_IMG;
                            message.obj = bitmap;
                            handler.sendMessage(message);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

        btnImgRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageRequest imageRequest = new ImageRequest(strUrl, new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        ivImg.setImageBitmap(bitmap);
                    }
                }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e(TAG, volleyError.toString());
                    }
                });
                requestQueue.add(imageRequest);
            }
        });

        btnImgLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageLoader imageLoader = new ImageLoader(requestQueue, imgLoadCache);

                ImageLoader.ImageListener listener = ImageLoader.getImageListener(ivImg, R.drawable.moren, R.drawable.moren);
                imageLoader.get(strUrl, listener);
            }
        });

        btnImgNetwork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageLoader imageLoader = new ImageLoader(requestQueue, imgLoadCache);

                ivNetwork.setDefaultImageResId(R.drawable.moren);
                ivNetwork.setErrorImageResId(R.drawable.moren);
                ivNetwork.setImageUrl(strUrl, imageLoader);
            }
        });
    }

    //绑定组件
    private void bindView() {
        btnHttpConnection = findViewById(R.id.btn_httpConnection);
        btnImgRequest = findViewById(R.id.btn_imgRequest);
        btnImgLoad = findViewById(R.id.btn_imgLoader);
        btnImgNetwork = findViewById(R.id.btn_imgNetwork);
        ivNetwork = findViewById(R.id.iv_network);
        ivImg = findViewById(R.id.iv_img);
    }
}
